#! /bin/sh /usr/share/dpatch/dpatch-run
## 81_sonames.dpatch by Mike Hommey <glandium@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Add soname support
## NB - move to so.12

@DPATCH@

--- mozilla/security/coreconf/BSD_OS.mk
+++ mozilla/security/coreconf/BSD_OS.mk
@@ -73,7 +73,7 @@
 DSO_CFLAGS		= -fPIC -DPIC
 DSO_LDOPTS		= -shared
 DSO_LDFLAGS		=
-DSO_LDOPTS		+= -Wl,-soname,lib$(LIBRARY_NAME)$(LIBRARY_VERSION).$(DLL_SUFFIX)
+DSO_LDOPTS		+= -Wl,-soname,$(SONAME)
 
 ifdef LIBRUNPATH
 DSO_LDOPTS		+= -Wl,-R$(LIBRUNPATH)
--- mozilla/security/coreconf/BeOS.mk
+++ mozilla/security/coreconf/BeOS.mk
@@ -59,7 +59,7 @@
 	CPU_ARCH	= x86
 endif
 
-MKSHLIB		= $(CC) -nostart -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB		= $(CC) -nostart -Wl,-soname -Wl,$(SONAME)
 ifdef BUILD_OPT
 	OPTIMIZER	= -O2
 endif
--- mozilla/security/coreconf/FreeBSD.mk
+++ mozilla/security/coreconf/FreeBSD.mk
@@ -51,7 +51,7 @@
 OS_CFLAGS		= $(DSO_CFLAGS) -ansi -Wall -DFREEBSD -DHAVE_STRERROR -DHAVE_BSD_FLOCK
 
 DSO_CFLAGS		= -fPIC
-DSO_LDOPTS		= -shared -Wl,-soname -Wl,$(notdir $@)
+DSO_LDOPTS		= -shared -Wl,-soname -Wl,$(SONAME)
 
 #
 # The default implementation strategy for FreeBSD is pthreads.
--- mozilla/security/coreconf/HP-UX.mk
+++ mozilla/security/coreconf/HP-UX.mk
@@ -83,7 +83,7 @@
 PROCESS_MAP_FILE = grep -v ';+' $< | grep -v ';-' | \
          sed -e 's; DATA ;;' -e 's,;;,,' -e 's,;.*,,' -e 's,^,+e ,' > $@
 
-DSO_LDOPTS		= -b +h $(notdir $@)
+DSO_LDOPTS		= -b +h $(SONAME)
 ifeq ($(OS_TEST),ia64)
 	DSO_LDOPTS	+= +b '$$ORIGIN'
 endif
--- mozilla/security/coreconf/IRIX.mk
+++ mozilla/security/coreconf/IRIX.mk
@@ -102,7 +102,7 @@
 	SHLIB_LD_OPTS	+= -n32 -mips3
 endif
 
-MKSHLIB     += $(LD) $(SHLIB_LD_OPTS) -shared -soname $(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB     += $(LD) $(SHLIB_LD_OPTS) -shared -soname $(SONAME)
 ifdef MAPFILE
 # Add LD options to restrict exported symbols to those in the map file
 endif
--- mozilla/security/coreconf/Linux.mk
+++ mozilla/security/coreconf/Linux.mk
@@ -139,7 +139,7 @@
 
 ifeq ($(KERNEL)-$(OS_RELEASE),linux-2.0)
 	OS_REL_CFLAGS	+= -DLINUX2_0
-	MKSHLIB		= $(CC) -shared -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+	MKSHLIB		= $(CC) -shared -Wl,-soname -Wl,$(SONAME)
 	ifdef MAPFILE
 		MKSHLIB += -Wl,--version-script,$(MAPFILE)
 	endif
--- mozilla/security/coreconf/Linux2.1.mk
+++ mozilla/security/coreconf/Linux2.1.mk
@@ -38,7 +38,7 @@
 include $(CORE_DEPTH)/coreconf/Linux.mk
 ifeq ($(OS_RELEASE),2.1)
         OS_REL_CFLAGS   += -DLINUX2_1
-        MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+        MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(SONAME)
 	ifdef MAPFILE
 		MKSHLIB += -Wl,--version-script,$(MAPFILE)
 	endif
--- mozilla/security/coreconf/Linux2.2.mk
+++ mozilla/security/coreconf/Linux2.2.mk
@@ -38,7 +38,7 @@
 include $(CORE_DEPTH)/coreconf/Linux.mk
 
 OS_REL_CFLAGS   += -DLINUX2_1
-MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(SONAME)
 
 ifdef MAPFILE
 	MKSHLIB += -Wl,--version-script,$(MAPFILE)
--- mozilla/security/coreconf/Linux2.4.mk
+++ mozilla/security/coreconf/Linux2.4.mk
@@ -38,7 +38,7 @@
 include $(CORE_DEPTH)/coreconf/Linux.mk
 
 OS_REL_CFLAGS   += -DLINUX2_1
-MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(SONAME)
 
 ifdef MAPFILE
 	MKSHLIB += -Wl,--version-script,$(MAPFILE)
--- mozilla/security/coreconf/Linux2.5.mk
+++ mozilla/security/coreconf/Linux2.5.mk
@@ -38,7 +38,7 @@
 include $(CORE_DEPTH)/coreconf/Linux.mk
 
 OS_REL_CFLAGS   += -DLINUX2_1
-MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(SONAME)
 
 ifdef MAPFILE
 	MKSHLIB += -Wl,--version-script,$(MAPFILE)
--- mozilla/security/coreconf/Linux2.6.mk
+++ mozilla/security/coreconf/Linux2.6.mk
@@ -40,7 +40,7 @@
 DSO_LDOPTS      += -Wl,-z,defs
 
 OS_REL_CFLAGS   += -DLINUX2_1
-MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB         = $(CC) $(DSO_LDOPTS) -Wl,-soname -Wl,$(SONAME)
 
 ifdef MAPFILE
 	MKSHLIB += -Wl,--version-script,$(MAPFILE)
--- mozilla/security/coreconf/NetBSD.mk
+++ mozilla/security/coreconf/NetBSD.mk
@@ -68,7 +68,7 @@
 DSO_LDOPTS		= -shared
 DSO_LDFLAGS		=
 ifeq ($(OBJECT_FMT),ELF)
-DSO_LDOPTS		+= -Wl,-soname,lib$(LIBRARY_NAME)$(LIBRARY_VERSION).$(DLL_SUFFIX)
+DSO_LDOPTS		+= -Wl,-soname,$(SONAME)
 endif
 
 ifdef LIBRUNPATH
--- mozilla/security/coreconf/OSF1.mk
+++ mozilla/security/coreconf/OSF1.mk
@@ -64,7 +64,7 @@
 endif
 
 # The command to build a shared library on OSF1.
-MKSHLIB    += ld -shared -expect_unresolved "*" -soname $(notdir $@)
+MKSHLIB    += ld -shared -expect_unresolved "*" -soname $(SONAME)
 ifdef MAPFILE
 MKSHLIB += -hidden -input $(MAPFILE)
 endif
--- mozilla/security/coreconf/OpenBSD.mk
+++ mozilla/security/coreconf/OpenBSD.mk
@@ -64,7 +64,7 @@
 ARCH			= openbsd
 
 DSO_CFLAGS		= -fPIC -DPIC
-DSO_LDOPTS		= -shared -fPIC -Wl,-soname,lib$(LIBRARY_NAME)$(LIBRARY_VERSION).$(DLL_SUFFIX)
+DSO_LDOPTS		= -shared -fPIC -Wl,-soname,$(SONAME)
 DSO_LDFLAGS		=
 
 MKSHLIB			= $(CC) $(DSO_LDOPTS)
--- mozilla/security/coreconf/QNX.mk
+++ mozilla/security/coreconf/QNX.mk
@@ -54,7 +54,7 @@
 	CPU_ARCH	= x86
 endif
 
-MKSHLIB		= $(CC) -shared -Wl,-soname -Wl,$(@:$(OBJDIR)/%.so=%.so)
+MKSHLIB		= $(CC) -shared -Wl,-soname -Wl,$(SONAME)
 ifdef BUILD_OPT
 	OPTIMIZER	= -O2
 endif
--- mozilla/security/coreconf/ReliantUNIX.mk
+++ mozilla/security/coreconf/ReliantUNIX.mk
@@ -51,7 +51,7 @@
 		OPTIMIZER += -O6
 	endif
 	MKSHLIB     = $(LD)
-	MKSHLIB    += -G -h $(@:$(OBJDIR)/%.so=%.so)
+	MKSHLIB    += -G -h $(SONAME)
 	DSO_LDOPTS += -G -Xlinker -Blargedynsym
 else
 	## native compiler (CDS++ 1.0)
--- mozilla/security/coreconf/SunOS5.mk
+++ mozilla/security/coreconf/SunOS5.mk
@@ -170,7 +170,7 @@
 ifeq ($(USE_64), 1)
 	DSO_LDOPTS += -m64
 endif
-	DSO_LDOPTS += -shared -h $(notdir $@)
+	DSO_LDOPTS += -shared -h $(SONAME)
 else
 ifeq ($(USE_64), 1)
 	ifeq ($(OS_TEST),i86pc)
@@ -179,7 +179,7 @@
 	    DSO_LDOPTS +=-xarch=v9
 	endif
 endif
-	DSO_LDOPTS += -G -h $(notdir $@)
+	DSO_LDOPTS += -G -h $(SONAME)
 endif
 DSO_LDOPTS += -z combreloc -z defs -z ignore

--- mozilla/security/coreconf/rules.mk
+++ mozilla/security/coreconf/rules.mk
@@ -119,6 +119,7 @@
 	$(INSTALL) -m 644 $(SHARED_LIBRARY:$(DLL_SUFFIX)=pdb) $(SOURCE_LIB_DIR)
 endif
 endif
+	$(call MKSHLINKS,$(SOURCE_LIB_DIR))
 endif
 ifdef IMPORT_LIBRARY
 	$(INSTALL) -m 775 $(IMPORT_LIBRARY) $(SOURCE_LIB_DIR)
@@ -268,6 +269,7 @@
 release_md::
 ifneq ($(MD_LIB_RELEASE_FILES),)
 	$(INSTALL) -m 444 $(MD_LIB_RELEASE_FILES) $(SOURCE_RELEASE_PREFIX)/$(SOURCE_RELEASE_LIB_DIR)
+	$(call MKSHLINKS,$(SOURCE_RELEASE_PREFIX)/$(SOURCE_RELEASE_LIB_DIR))
 endif
 ifneq ($(MD_BIN_RELEASE_FILES),)
 	$(INSTALL) -m 555 $(MD_BIN_RELEASE_FILES) $(SOURCE_RELEASE_PREFIX)/$(SOURCE_RELEASE_BIN_DIR)
@@ -328,6 +330,8 @@
 endif
 endif
 
+$(SHARED_LIBRARY_LINKS): %: $(SHARED_LIBRARY)
+
 $(SHARED_LIBRARY): $(OBJS) $(RES) $(MAPFILE) $(SUB_SHLOBJS)
 	@$(MAKE_OBJDIR)
 	rm -f $@
@@ -355,6 +356,7 @@
 else
 ifdef XP_OS2_VACPP
 	$(MKSHLIB) $(DLLFLAGS) $(LDFLAGS) $(OBJS) $(SUB_SHLOBJS) $(LD_LIBS) $(EXTRA_LIBS) $(EXTRA_SHARED_LIBS) $(OS_LIBS)
+	$(call MKSHLINKS,.)
 else
 	$(MKSHLIB) -o $@ $(OBJS) $(SUB_SHLOBJS) $(LD_LIBS) $(EXTRA_LIBS) $(EXTRA_SHARED_LIBS) $(OS_LIBS)
 endif
--- mozilla/security/coreconf/ruleset.mk
+++ mozilla/security/coreconf/ruleset.mk
@@ -123,6 +123,25 @@
     ifndef SHARED_LIBRARY
 	SHARED_LIBRARY = $(OBJDIR)/$(DLL_PREFIX)$(LIBRARY_NAME)$(LIBRARY_VERSION)$(JDK_DEBUG_SUFFIX).$(DLL_SUFFIX)
     endif
+    SONAME = $(notdir $(SHARED_LIBRARY))
+    ifdef SO_VERSION
+    ifneq (,$(findstring $(SONAME),$(MKSHLIB)))
+        SO_VERSION_MAJOR	:= $(shell echo $(SO_VERSION) | sed 's/^\([^.]*\)\(\.[^.]*\)\?\(\.[^.]*\)\?/\1/')
+        SO_VERSION_MINOR	:= $(shell echo $(SO_VERSION) | sed 's/^\([^.]*\)\(\.[^.]*\)\?\(\.[^.]*\)\?/\2/')
+        SO_VERSION_MICRO	:= $(shell echo $(SO_VERSION) | sed 's/^\([^.]*\)\(\.[^.]*\)\?\(\.[^.]*\)\?/\3/')
+
+        SHARED_LIBRARY_LINKS	:= $(SONAME)
+        ifdef SO_VERSION_MINOR
+            SHARED_LIBRARY_LINKS	+= $(SONAME).$(SO_VERSION_MAJOR)
+        endif
+
+        SONAME			:= $(SONAME).$(SO_VERSION_MAJOR)
+        SHARED_LIBRARY		:= $(SHARED_LIBRARY).$(SO_VERSION)
+
+        MKSHLINKS		= (cd $(1) && for link in $(SHARED_LIBRARY_LINKS); do rm -f $$link; ln -s $(notdir $(SHARED_LIBRARY)) $$link; done)
+    endif
+    endif
+
     ifndef MAPFILE_SOURCE
 	MAPFILE_SOURCE = $(LIBRARY_NAME).def
     endif
@@ -189,7 +189,7 @@
 endif
 
 ALL_TRASH =	$(TARGETS) $(OBJS) $(OBJDIR) LOGS TAGS $(GARBAGE) \
-		so_locations $(BUILT_SRCS) $(NOSUCHFILE)
+		so_locations $(BUILT_SRCS) $(NOSUCHFILE) $(SHARED_LIBRARY_LINKS)
 
 ifdef NS_USE_JDK
     ALL_TRASH += $(JDK_HEADER_CFILES) $(JDK_STUB_CFILES) \
--- mozilla/security/nss/lib/nss/manifest.mn
+++ mozilla/security/nss/lib/nss/manifest.mn
@@ -57,3 +57,4 @@
 
 LIBRARY_NAME = nss
 LIBRARY_VERSION = 3
+SO_VERSION = 12
--- mozilla/security/nss/lib/smime/manifest.mn
+++ mozilla/security/nss/lib/smime/manifest.mn
@@ -79,3 +79,4 @@
 
 LIBRARY_NAME = smime
 LIBRARY_VERSION = 3
+SO_VERSION = 12
--- mozilla/security/nss/lib/softoken/manifest.mn
+++ mozilla/security/nss/lib/softoken/manifest.mn
@@ -43,6 +43,7 @@
 LIBRARY_NAME = softokn
 LIBRARY_VERSION = 3
 MAPFILE = $(OBJDIR)/softokn.def
+SO_VERSION = 12
 
 DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" -DSOFTOKEN_LIB_NAME=\"$(notdir $(SHARED_LIBRARY))\"
 
--- mozilla/security/nss/lib/ssl/manifest.mn
+++ mozilla/security/nss/lib/ssl/manifest.mn
@@ -86,4 +86,4 @@
 
 LIBRARY_NAME = ssl
 LIBRARY_VERSION = 3
-
+SO_VERSION = 12
--- mozilla/security/nss/lib/freebl/shvfy.c
+++ mozilla/security/nss/lib/freebl/shvfy.c
@@ -50,16 +50,21 @@
 mkCheckFileName(const char *libName)
 {
     int ln_len = PORT_Strlen(libName);
+    int len = ln_len;
     char *output = PORT_Alloc(ln_len+sizeof(SGN_SUFFIX));
-    int index = ln_len + 1 - sizeof("."SHLIB_SUFFIX);
+    char *ext = PORT_Strstr(libName, "."SHLIB_SUFFIX);
 
-    if ((index > 0) &&
-        (PORT_Strncmp(&libName[index],
-                        "."SHLIB_SUFFIX,sizeof("."SHLIB_SUFFIX)) == 0)) {
-        ln_len = index;
+    if (ext != NULL)
+        len = ext - libName;
+
+    PORT_Memcpy(output,libName,len);
+
+    if ((ext != NULL) && (libName[len + sizeof(SHLIB_SUFFIX)] != 0)) {
+        PORT_Memcpy(&output[len],&libName[len + sizeof(SHLIB_SUFFIX)], ln_len - sizeof(SHLIB_SUFFIX) - len);
+        len = ln_len - sizeof(SHLIB_SUFFIX);
     }
-    PORT_Memcpy(output,libName,ln_len);
-    PORT_Memcpy(&output[ln_len],SGN_SUFFIX,sizeof(SGN_SUFFIX));
+
+    PORT_Memcpy(&output[len],SGN_SUFFIX,sizeof(SGN_SUFFIX));
     return output;
 }
 
--- mozilla/security/nss/cmd/shlibsign/shlibsign.c
+++ mozilla/security/nss/cmd/shlibsign/shlibsign.c
@@ -77,16 +77,21 @@
 mkoutput(const char *input)
 {
     int in_len = PORT_Strlen(input);
+    int len = in_len;
     char *output = PORT_Alloc(in_len+sizeof(SGN_SUFFIX));
-    int index = in_len + 1 - sizeof("."SHLIB_SUFFIX);
+    char *ext = PORT_Strstr(input, "."SHLIB_SUFFIX);
 
-    if ((index > 0) && 
-	(PORT_Strncmp(&input[index],
-			"."SHLIB_SUFFIX,sizeof("."SHLIB_SUFFIX)) == 0)) {
-	in_len = index;
+    if (ext != NULL)
+        len = ext - input;
+
+    PORT_Memcpy(output,input,len);
+
+    if ((ext != NULL) && (input[len + sizeof(SHLIB_SUFFIX)] != 0)) {
+        PORT_Memcpy(&output[len],&input[len + sizeof(SHLIB_SUFFIX)], in_len - sizeof(SHLIB_SUFFIX) - len);
+        len = in_len - sizeof(SHLIB_SUFFIX);
     }
-    PORT_Memcpy(output,input,in_len);
-    PORT_Memcpy(&output[in_len],SGN_SUFFIX,sizeof(SGN_SUFFIX));
+
+    PORT_Memcpy(&output[len],SGN_SUFFIX,sizeof(SGN_SUFFIX));
     return output;
 }
 
